<?php

//订单功能


namespace App\Http\Controllers\Property;


use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Fixtures\Prophecy\EmptyClass;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class GoodsController extends Controller{

    //总和
    public function Sum($id){
        $data      = DB::table('order')
            ->where('order_goodsname',$id)
            ->count('order_goodsname');
        return $data;
    }

    //数量
    public function quantity($id){
        $data      = DB::table('order')
            ->select('quantity')
            ->where('order_goodsname',$id)
            ->get();
        return $data;
    }

    //商品类型
    public function PanicBuying($village_id,$take,$where){
        $PanicBuying     = DB::table('goods')
            ->select('goods_id','goodsname','typename','unit','goodsname','goodsimage','price','discounted_price',
                'stock','description','goods_create_time','price_two','price_three','activity_time','activity_end')
            ->leftjoin('goodstype','goods.type_goods','=','goodstype.type_id')
            ->leftjoin('unit','goods.unit_goods','=','unit.unit_id')
            ->skip(0)->take($take)
            ->where('type_goods','=',$where)
            ->where('goods_village','=',$village_id)
            ->where('goods_operation','=',0)
            ->orderby('goods_id','ase')
            ->get();
        return $PanicBuying;
    }

    //抢购说明
    public function getwelfare($village_id){
        $PanicBuying    = $this ->PanicBuying($village_id,3,5);
        foreach ($PanicBuying as $k => $v){
            $goodsname      = $v ->goodsname;
            $typename       = $v ->typename;
            $price          = $v->price;
            // $discounted_price = $v->discounted_price;
            $price_two        = $v ->price_two;
            $price_three      = $v ->price_three;
            $stock            = $v->stock;
            $description      = $v ->description;
            $goods_id         = $v->goods_id;
            $timestart             = strtotime($v->activity_time);
            $timeend               = strtotime($v->activity_end);
            $timeend               = $v->activity_end;
            $dat                  =  date('Y-m-d H:i:s');
            $timesend                 = strtotime($timeend)-strtotime($dat);
         //   $timestart        = strtotime($v ->activity_time);
          //  $timesend         = strtotime($v ->activity_end);

        //    $times            = ceil(($timesend-$timestart));
           // $time             = strtotime($v->goods_create_time);
            $sum              = $this ->Sum($goodsname);
            $quantity         = $this ->quantity($goodsname);
            foreach ($quantity as $key){
                $id[]   = $key ->quantity;
                $dd     = max($id);
            }
            if(empty($dd)){
                $arr = 0;
            }elseif ($dd>0 && $dd<=1){
                $arr = 1;
            }elseif ($dd>=1 && $dd<=2){
                $arr = 2;
            }else{
                $arr = 3;
            }
            // $arr['quantity'] = $dd;
            if(empty($v ->goodsimage)){
                $temp  = [];
            }else {
                $temp = explode(',',$v ->goodsimage);
            }
            $data[]         = array('stage'=>$arr,'time'=>$timesend*1000,'goods_id'=>$goods_id,'img_str'=>$temp,
                'typename'=>$typename,'goodsname'=>$goodsname,
                'price'=>$price,'price_two'=>$price_two,'price_three'=>$price_three,
                'stock'=>$stock,'description'=>$description,
                'sum'  =>$sum
                );
        }
        if(empty($data)){
            return array();
        }else{
            return $data;
        }
    }

    public function welfare($village_id){
        $PanicBuying     = $this ->PanicBuying($village_id,1,2);
        $DomesticServices     = $this ->PanicBuying($village_id,2,1);
        $data   = array_merge($PanicBuying,$DomesticServices);
        foreach ($data as $k => $v){
            $goodsname      = $v ->goodsname;
            $goods_id       = $v->goods_id;
            $typename       = $v ->typename;
            $price          = $v->price;
            $discounted_price = $v->discounted_price;
            $stock            = $v->stock;
            $description      = $v ->description;
            if(empty($v ->goodsimage)){
                $temp  = [];
            }else {
                $temp = explode(',',$v ->goodsimage);
            }
            $time             = strtotime($v->goods_create_time);
            $sum              = $this ->Sum($goodsname);
            $arr[$k]['sum']           = $sum;
            $arr[$k]['time'] = $time;
            $arr[$k]['goods_id'] = $goods_id;
            $arr[$k]['img_str'] = $temp;
            $arr[$k]['typename']=$typename;
            $arr[$k]['goodsname']= $goodsname;
            $arr[$k]['price']     = $price;
            $arr[$k]['discounted_price'] = $discounted_price;
            $arr[$k]['stock']  = $stock;
            $arr[$k]['description']  = $description;
        }
        if(empty($arr)){
            return array();
        }else{
            return $arr;
        }
    }



    //主页
    public function Home(Request $request){
        if($request ->isMethod('post')) {
            $id      = $request ->input('village_id');
            $select  = DB::table('goods') ->where('goods_village',$id)->where('goods_operation',0) ->get();
            if($select == true){
                $panicbuying  = $this ->PanicBuying($id,2,2);
                foreach ($panicbuying as $k => $v){
                    $goods_id       = $v->goods_id;
                    $goodsname      = $v ->goodsname;
                    $typename       = $v ->typename;
                    $price          = $v->price;
                    $discounted_price = $v->discounted_price;
                    $stock            = $v->stock;
                    $description      = $v ->description;
                    if(empty($v ->goodsimage)){
                        $temp  = [];
                    }else {
                        $temp = explode(',',$v ->goodsimage);
                    }
                 //   $timestart             = strtotime($v->activity_time);
                   // $timeend               = strtotime($v->activity_end);
                    $timeend               = $v->activity_end;
                    $dat                  =  date('Y-m-d H:i:s');
                    $timesend                 = strtotime($timeend)-strtotime($dat);
                    $sum              = $this ->Sum($goodsname);
                    $arr[$k]['time'] = $timesend*1000;
                    $arr[$k]['goods_id'] = $goods_id;
                    $arr[$k]['img_str'] = $temp;
                    $arr[$k]['typename']=$typename;
                    $arr[$k]['goodsname']= $goodsname;
                    $arr[$k]['price']     = $price;
                    $arr[$k]['discounted_price'] = $discounted_price;
                    $arr[$k]['stock']  = $stock;
                    $arr[$k]['description']  = $description;
                    $arr[$k]['sum']           = $sum;
                }
                if(empty($arr)){
                    $arrk = array();
                }else{
                    $arrk = $arr;
                }
                $DomesticServices  = $this->PanicBuying($id,2,1);
                foreach ($DomesticServices as $k3 => $v3){
                    $goods_id       = $v3->goods_id;
                    $goodsname      = $v3 ->goodsname;
                    $typename       = $v3 ->typename;
                    $price          = $v3->price;
                    $discounted_price = $v3->discounted_price;
                    $stock            = $v3->stock;
                    $description      = $v3 ->description;
                    if(empty($v3 ->goodsimage)){
                        $temps  = [];
                    }else {
                        $temps = explode(',',$v3 ->goodsimage);
                    }
                    $sum              = $this ->Sum($goodsname);
                    $timeend               = $v3->activity_end;
                    $dat                  =  date('Y-m-d H:i:s');
                    $timesend                 = strtotime($timeend)-strtotime($dat);
                    $arr_a[$k3]['goods_id'] = $goods_id;
                    $arr_a[$k3]['time'] = $timesend;
                    $arr_a[$k3]['img_str'] = $temps;
                    $arr_a[$k3]['typename']=$typename;
                    $arr_a[$k3]['goodsname']= $goodsname;
                    $arr_a[$k3]['price']     = $price;
                    $arr_a[$k3]['discounted_price'] = $discounted_price;
                    $arr_a[$k3]['stock']  = $stock;
                    $arr_a[$k3]['description']  = $description;
                    $arr_a[$k3]['sum']           = $sum;
                }
                if(empty($arr_a)){
                    $arr_ak = array();
                }else{
                    $arr_ak = $arr_a;
                }

                $value = array(
                    'getwelfare' => $this->getwelfare($id),
                    //'welfare' => $this->welfare($id),
                    'panicbuying' => $arrk,
                    'DomesticServices' =>$arr_ak
                );

                return json::json('success', '获取全部商品信息', $value);
            } else {
                return json::json('failure', '由于网络或者不正当原因注册失败', '');
            }
        }
    }

    public function More(Request $request,$type){
        if($request ->isMethod('post')){
            $village_id      = $request ->input('village_id');
            if($type ==1){
                //生活服务更多
                $DomesticServices    = DB::table('goods')
                    ->select('goods_id','goodsname','typename','unit','goodsname','goodsimage','price','discounted_price',
                        'stock','description','goods_create_time')
                    ->leftjoin('goodstype','goods.type_goods','=','goodstype.type_id')
                    ->leftjoin('unit','goods.unit_goods','=','unit.unit_id')
                    ->where('type_goods',1)
                    ->where('goods_operation',0)
                    ->where('goods_village',$village_id)
                    ->get();
                foreach ($DomesticServices as $k3 => $v3){
                    $goods_id       = $v3->goods_id;
                    $goodsname      = $v3 ->goodsname;
                    $typename       = $v3 ->typename;
                    $price          = $v3->price;
                    $discounted_price = $v3->discounted_price;
                    $stock            = $v3->stock;
                    $description      = $v3 ->description;
                    if(empty($v3 ->goodsimage)){
                        $temps  = [];
                    }else {
                        $temps = explode(',',$v3 ->goodsimage);
                    }
                    $sum              = $this ->Sum($goodsname);
                    $time             = strtotime($v3->goods_create_time);
                    $arr_a[$k3]['time'] = $time;
                    $arr_a[$k3]['goods_id'] = $goods_id;
                    $arr_a[$k3]['img_str'] = $temps;
                    $arr_a[$k3]['typename']=$typename;
                    $arr_a[$k3]['goodsname']= $goodsname;
                    $arr_a[$k3]['price']     = $price;
                    $arr_a[$k3]['discounted_price'] = $discounted_price;
                    $arr_a[$k3]['stock']  = $stock;
                    $arr_a[$k3]['description']  = $description;
                    $arr_a[$k3]['sum']           = $sum;
                }
                if(empty($arr_a)){
                    $dc = array();
                }else{
                    foreach(array_chunk($arr_a, 2) as $val)
                    {
                        $dc[] = $val;
                    }
                }


                if ($DomesticServices == true) {
                    return json::json('success', '获取更多商品信息', $dc);
                } else {
                    return json::json('failure', '由于网络或者不正当原因注册失败', []);
                }
            }elseif($type ==2){
                //生活服务
                $PanicBuying     = DB::table('goods')
                    ->select('goods_id','goodsname','typename','unit','goodsname','goodsimage','price','discounted_price',
                        'stock','description','goods_create_time')
                    ->leftjoin('goodstype','goods.type_goods','=','goodstype.type_id')
                    ->leftjoin('unit','goods.unit_goods','=','unit.unit_id')
                    ->where('type_goods',2)
                    ->where('goods_operation',0)
                    ->where('goods_village',$village_id)
                    ->get();
                foreach ($PanicBuying as $k => $v){
                    $goods_id       = $v->goods_id;
                    $goodsname      = $v ->goodsname;
                    $typename       = $v ->typename;
                    $price          = $v->price;
                    $discounted_price = $v->discounted_price;
                    $stock            = $v->stock;
                    $description      = $v ->description;
                    if(empty($v ->goodsimage)){
                        $temp  = [];
                    }else {
                        $temp = explode(',',$v ->goodsimage);
                    }
                    $time             = $v->goods_create_time;
                    $sum              = $this ->Sum($goodsname);
                    $arr[$k]['sum']           = $sum;
                    $arr[$k]['time'] = $time;
                    $arr[$k]['goods_id'] = $goods_id;
                    $arr[$k]['img_str'] = $temp;
                    $arr[$k]['typename']=$typename;
                    $arr[$k]['goodsname']= $goodsname;
                    $arr[$k]['price']     = $price;
                    $arr[$k]['discounted_price'] = $discounted_price;
                    $arr[$k]['stock']  = $stock;
                    $arr[$k]['description']  = $description;
                    $arr[$k]['sum']           = $sum;
                }
                if(empty($arr)){
                    $dc = array();
                }else{
                    foreach(array_chunk($arr, 2) as $val)
                    {
                        $dc[] = $val;
                    }
                }
                if ($PanicBuying == true) {
                    return json::json('success', '获取更多商品信息', $dc);
                } else {
                    return json::json('failure', '由于网络或者不正当原因注册失败', []);
                }
            }else{
                //生活服务
                $PanicBuying     = DB::table('goods')
                    ->select('goods_id','goodsname','typename','unit','goodsname','goodsimage','price','discounted_price',
                        'stock','description','goods_create_time')
                    ->leftjoin('goodstype','goods.type_goods','=','goodstype.type_id')
                    ->leftjoin('unit','goods.unit_goods','=','unit.unit_id')
                    ->where('type_goods',5)
                    ->where('goods_operation',0)
                    ->where('goods_village',$village_id)
                    ->get();
                foreach ($PanicBuying as $k => $v){
                    $goods_id       = $v->goods_id;
                    $goodsname      = $v ->goodsname;
                    $typename       = $v ->typename;
                    $price          = $v->price;
                    $discounted_price = $v->discounted_price;
                    $stock            = $v->stock;
                    $description      = $v ->description;
                    if(empty($v ->goodsimage)){
                        $temp  = [];
                    }else {
                        $temp = explode(',',$v ->goodsimage);
                    }
                    $time             = $v->goods_create_time;
                    $sum              = $this ->Sum($goodsname);
                    $arr[$k]['sum']           = $sum;
                    $arr[$k]['time'] = $time;
                    $arr[$k]['goods_id'] = $goods_id;
                    $arr[$k]['img_str'] = $temp;
                    $arr[$k]['typename']=$typename;
                    $arr[$k]['goodsname']= $goodsname;
                    $arr[$k]['price']     = $price;
                    $arr[$k]['discounted_price'] = $discounted_price;
                    $arr[$k]['stock']  = $stock;
                    $arr[$k]['description']  = $description;
                    $arr[$k]['sum']           = $sum;
                }
                if(empty($arr)){
                    $dc = array();
                }else{
                    foreach(array_chunk($arr, 2) as $val)
                    {
                        $dc[] = $val;
                    }
                }
                if ($PanicBuying == true) {
                    return json::json('success', '获取更多商品信息', $dc);
                } else {
                    return json::json('failure', '由于网络或者不正当原因注册失败', []);
                }
            }
        }

    }




    //生成订单
    public function getOrder(Request $request){
        if ($request ->isMethod('post')){
            $user_id     = $request ->input('user_id');
            $order       = sha1(rand(1000,9999)."wuyu".date('Ymdhis'));
            $goodsname   = $request ->input('goodsname');
            $address     = $request ->input('address_id');
            $sum         = $request ->input('sum');
            $quantity    = $request ->input('quantity');
            $creation_time  =  date('Y-m-d H:i:s');
            $data        = DB::table('order')
                ->select('goodsname','address_id','quantity','sum','order','user_order')
                ->insert([
                    'user_order' => $user_id,
                    'order'     => $order,
                    'goodsname' => $goodsname,
                    'address_id' => $address,
                    'sum'         => $sum,
                    'quantity'   => $quantity,
                    'creation_time'  => $creation_time
                ]);
            if($data == true){
                return json::json('success', '支付成功', '');
            }


        }
    }

    //小区生活
    public function hottopic(Request $request){
        if($request ->isMethod('post')) {
            $village_id      = $request ->input('village_id');
            $data = DB::table('hottopic')
                ->select('hottopic_id','hottopic_title', 'hottopic_image')
                ->skip(0)->take(1)
                ->where('goods_village',$village_id)
                ->orderby('hottopic_id','ASE')
                ->get();
            foreach ($data as $value){
                if(empty($value ->hottopic_image)){
                    $hottopic_image  = [];
                }else {
                    $hottopic_image = explode(',',$value ->hottopic_image);
                }
                $id  = $value -> hottopic_id;
                $hottopic_title  = $value ->hottopic_title;

                $da  = array('hottopic_id' =>$id,'hottopic_title'=>$hottopic_title,'hottopic_image'=>$hottopic_image);
            }
        }
        if ($data == true) {
            return json::json('success', '获取小区生活', $da);
        } else {
            return json::json('failure', '由于网络或者不正当原因失败', '');
        }
    }

    //购买记录
    public function purchase_record($id){
        $data     = DB::table('order')
            ->where('order_goodsname',$id)
            ->get();
        return $data;

    }

    //商品详情
    public function GoodsDetails(Request $request){
        if($request ->isMethod('post')){
            $goods_id        = $request ->input('goods_id');
            $data            = DB::table('goods')
                ->leftjoin('order','goods.goods_id','=','order.order_goods_id')
                //->leftjoin('goodstype','goods.type_goods','=','goodstype.goodstype_id')
                ->leftjoin('user','order.user_order','=','user.user_id')
                ->where('goods_id',$goods_id)
                // ->where('order_status',0)
                //->skip(0)->take(3)
                ->get();
            foreach ($data as $value){
                $ids         = $value ->goods_id;
                $image      = explode(',',$value ->goodsimage);
                $url        ="http://www.mukitech.com/wuye/app/public/property/explain/$goods_id";
                $particulars ="http://www.mukitech.com/wuye/app/public/property/particulars/$goods_id";
                header("Content-Type: text/html; charset=utf-8");
                $html_content = file_get_contents($particulars);
                $goodsname   = $value ->goodsname;
                $price       = $value ->price;
                $head        = $value ->head;
                $nickname    = $value ->nickname;
                $type_goods  = $value ->type_goods;
                $discounted_price = $value ->discounted_price;
                $time        = $value ->order_create_time;
                $activitytime = strtotime($value->activity_time);
                $sum          = $this->Sum($goodsname);
                $producing_area  = $value ->producing_area;
                $distribution    = $value ->distribution;
                $price_two       = $value ->price_two;
                $price_three     = $value ->price_three;
                if(empty($value ->goodsimage)){
                    $goodsimage  = [];
                }else {
                    $goodsimage = explode(',', $value->goodsimage);
                }
                if($head =="" && $nickname=="" & $time=="")
                {
                    $parist     = [];
                }else{
                    $parist[]    = array('head'=>$head,'nickname'=>$nickname,'time'=>$time);
                }

                $da         = array('goods_id'=>$ids,'type_goods'=>$type_goods,'image'=>$image,'goodsname'=>$goodsname,'price'=>$price,
                    'discounted_price'=>$discounted_price,'url'=>$url,'particulars'=>$html_content,
                    'price_two'=>$price_two,'price_three'=>$price_three,'activity_time'=>$activitytime,
                    'producing_area' =>$producing_area,'distribution'=>$distribution,'goodsimage'=>$goodsimage,
                    'list'=>$parist,'sum'=>$sum);
            }
            if ($data == true) {
                return json::json('success', '获取商品详情成功', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因注册失败', '');
            }

        }
    }

    public function AllOrder(Request $request){
        if($request ->isMethod('post')){
            $goods_id        = $request ->input('goods_id');
            $data            = DB::table('goods')
                ->select('nickname','head','order_create_time')
                ->leftjoin('order','goods.goodsname','=','order.order_goodsname')
                ->leftjoin('user','order.user_order','=','user.user_id')
                ->where('goods_id',$goods_id)
                ->get();
            if ($data == true) {
                return json::json('success', '获取全部购买本产品的客户成功', $data);
            } else {
                return json::json('failure', '由于网络或者不正当原因注册失败', '');
            }
        }

    }




    public function GoodsMore(Request $request){
        if($request ->isMethod('post')){
            $village_id      = $request ->input('village_id');
            $PanicBuying     = DB::table('goods')
                ->select('goods_id','goodsname','typename','unit','goodsname','goodsimage','price','discounted_price',
                    'price_two','price_three',
                    'stock','description','goods_create_time')
                ->leftjoin('goodstype','goods.type_goods','=','goodstype.type_id')
                ->leftjoin('unit','goods.unit_goods','=','unit.unit_id')
                ->where('type_goods',5)
                ->where('goods_operation',0)
                ->where('goods_village',$village_id)
                ->get();
            foreach ($PanicBuying as $k => $v){
                $goods_id       = $v->goods_id;
                $goodsname      = $v ->goodsname;
                $typename       = $v ->typename;
                $price          = $v->price;
                $discounted_price = $v->discounted_price;
                $stock            = $v->stock;
                $description      = $v ->description;
                $price_two        = $v ->price_two;
                $price_three      = $v ->price_three;
                if(empty($v ->goodsimage)){
                    $temp  = [];
                }else {
                    $temp = explode(',',$v ->goodsimage);
                }
                $time             = $v->goods_create_time;
                $sum              = $this ->Sum($goodsname);
                $arr[$k]['sum']           = $sum;
                $arr[$k]['time'] = $time;
                $arr[$k]['goods_id'] = $goods_id;
                $arr[$k]['img_str'] = $temp;
                $arr[$k]['typename']=$typename;
                $arr[$k]['goodsname']= $goodsname;
                $arr[$k]['price']     = $price;
                $arr[$k]['discounted_price'] = $discounted_price;
                $arr[$k]['price_two']  = $price_two;
                $arr[$k]['price_three']  = $price_three;
                $arr[$k]['stock']  = $stock;
                $arr[$k]['description']  = $description;
                $arr[$k]['sum']           = $sum;
            }
            if(empty($arr)){
                $dc = array();
            }else{
                foreach(array_chunk($arr, 2) as $val)
                {
                    $dc[] = $val;
                }
            }
            if ($PanicBuying == true) {
                return json::json('success', '获取更多商品信息', $dc);
            } else {
                return json::json('failure', '由于网络或者不正当原因注册失败', []);
            }
        }
    }


}